package com.qining618.cms.ucenter.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qining618.cms.ucenter.controller.admin.request.AdminAdminAddRequest;
import com.qining618.cms.ucenter.controller.admin.request.AdminAdminEditRequest;
import com.qining618.cms.ucenter.controller.admin.request.FreezeRequest;
import com.qining618.cms.ucenter.controller.admin.request.ResetPasswordRequest;
import com.qining618.cms.ucenter.domain.User;
import com.qining618.cms.ucenter.service.AdminService;
import com.qining618.common.annotation.Access;
import com.qining618.common.result.ApiResult;
import com.qining618.common.result.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author flt
 * @date 2019-09-21
 */
@RestController
@RequestMapping("/admin/api/ucenter/admin")
public class AdminAdminController {

    @Autowired
    private AdminService adminService;

    @Access(needAdmin = true)
    @GetMapping(value = "page")
    @Description("分页")
    public ApiResult page(@RequestParam Map<String, Object> params) {
        IPage<User> pageInfo = adminService.page(params);

        return ApiResult.ok(TableData.of(pageInfo));
    }

    @Access(needAdmin = true)
    @PostMapping
    @Description("添加")
    public ApiResult add(@RequestBody @Validated AdminAdminAddRequest adminAddRequest) {
        adminService.add(adminAddRequest.getNickname(), adminAddRequest.getUsername(),
                adminAddRequest.getPassword(), adminAddRequest.getRoleId());
        return ApiResult.ok();
    }

    @Access(needAdmin = true)
    @PutMapping(value = "{id}")
    @Description("修改")
    public ApiResult modify(
            @PathVariable(value = "id") Long id,
            @RequestBody @Validated AdminAdminEditRequest adminEditRequest) {
        adminService.modify(id, adminEditRequest.getNickname(), adminEditRequest.getUsername(),
                adminEditRequest.getRoleId());
        return ApiResult.ok();
    }

    @Access(needAdmin = true)
    @PutMapping(value = "{id}/resetPassword")
    @Description("重置密码")
    public ApiResult resetPassword(
            @PathVariable(value = "id") Long id,
            @RequestBody @Validated ResetPasswordRequest resetPasswordRequest) {
        adminService.resetPassword(id, resetPasswordRequest.getPassword());
        return ApiResult.ok();
    }

    @Access(needAdmin = true)
    @PostMapping("{id}/freeze")
    @Description("冻结")
    public ApiResult freeze(
            @PathVariable("id") Long userId,
            @RequestBody @Validated FreezeRequest freezeRequest) {
        adminService.freeze(userId, freezeRequest.getDueTime(), freezeRequest.getReason());
        return ApiResult.ok();
    }

    @Access(needAdmin = true)
    @PostMapping("{id}/unFreeze")
    @Description("解冻")
    public ApiResult unFreeze(@PathVariable("id") Long userId) {
        adminService.unFreeze(userId);
        return ApiResult.ok();
    }

}
